package xyz.kail.demo.easyrules.mvel;

import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rule;
import org.jeasy.rules.api.Rules;
import org.jeasy.rules.api.RulesEngineParameters;
import org.jeasy.rules.core.AbstractRulesEngine;
import org.jeasy.rules.core.DefaultRulesEngine;
import org.jeasy.rules.mvel.MVELRule;

public class Main {
    public static void main(String[] args) {
        Rule ageRule = new MVELRule()
                .name("age rule")
                .description("Check if person's age is > 18 and marks the person as adult")
                .priority(1)
                .when("person.getAge() > 18")
                .then("person.setAdult(true);")
                .then("System.out.println(person);");

        Rules rules = new Rules();
        rules.register(ageRule);

        Person person = new Person();
        person.setAge(20);

        Facts facts = new Facts();
        facts.put("person", person);


        // 规则引擎
        AbstractRulesEngine rulesEngine = new DefaultRulesEngine();

        // 触发规则
        rulesEngine.fire(rules, facts);
    }

    public static final class Person {

        private Integer age;

        private boolean adult;

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public boolean isAdult() {
            return adult;
        }

        public void setAdult(boolean adult) {
            this.adult = adult;
        }

        @Override
        public String toString() {
            return "Person{" +
                    "age=" + age +
                    ", adult=" + adult +
                    '}';
        }
    }
}